<?php
namespace Home\Controller;

use Think\Controller;

class PageController extends BaseController
{
    
    // 展示某个项目的单个页面
    public function index()
    {
        import("Vendor.Parsedown.Parsedown");
        $page_id = I("page_id/d");
        $page = D("Page")->where(" page_id = '$page_id' ")->find();
        $login_user = $this->checkLogin(false);
        if (! $this->checkItemVisit($login_user['uid'], $page['item_id'])) {
            $this->message("你无权限");
            return;
        }
        $Parsedown = new \Parsedown();
        // $page['page_content'] = $Parsedown->text(htmlspecialchars_decode($page['page_content']));
        $this->assign("page", $page);
        $this->display();
    }
    
    // 返回单个页面的源markdown代码
    public function md()
    {
        $page_id = I("page_id/d");
        $page = D("Page")->where(" page_id = '$page_id' ")->find();
        echo $page['page_content'];
    }
    
    // 编辑页面
    public function edit()
    {
        $login_user = $this->checkLogin();
        $page_id = I("page_id/d");
        $item_id = I("item_id/d");
        
        $page_history_id = I("page_history_id/d");
        $copy_page_id = I("copy_page_id/d");
        
        if ($page_id > 0) {
            if ($page_history_id) {
                $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
            } else {
                $page = D("Page")->where(" page_id = '$page_id' ")->find();
            }
            $default_cat_id = $page['cat_id'];
        } // 如果是复制接口
elseif ($copy_page_id) {
            $copy_page = D("Page")->where(" page_id = '$copy_page_id' ")->find();
            $page['page_title'] = $copy_page['page_title'] . "-副本";
            $page['page_content'] = $copy_page['page_content'];
            $page['item_id'] = $copy_page['item_id'];
            $default_cat_id = $copy_page['cat_id'];
        } else {
            // 查找用户上一次设置的目录
            $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and $item_id = '$item_id' ")
                ->order(" addtime desc ")
                ->limit(1)
                ->find();
            $default_cat_id = $last_page['cat_id'];
        }
        
        $item_id = $page['item_id'] ? $page['item_id'] : $item_id;
        
        if (! $this->checkItemPermn($login_user['uid'], $item_id)) {
            $this->message("你无权限");
            return;
        }
        
        $this->assign("page", $page);
        $this->assign("item_id", $item_id);
        $this->assign("default_cat_id", $default_cat_id);
        
        $this->display();
    }
    
    // 保存
    public function save()
    {
        $login_user = $this->checkLogin();
        $page_id = I("page_id/d") ? I("page_id/d") : 0;
        $page_title = I("page_title") ? I("page_title") : '默认页面';
        $page_content = I("page_content");
        $cat_id = I("cat_id/d") ? I("cat_id/d") : 0;
        $item_id = I("item_id/d") ? I("item_id/d") : 0;
        $order = I("order/d") ? I("order/d") : 99;
        
        $login_user = $this->checkLogin();
        if (! $this->checkItemPermn($login_user['uid'], $item_id)) {
            $this->message("你无权限");
            return;
        }
        
        $data['page_title'] = $page_title;
        $data['page_content'] = $page_content;
        $data['order'] = $order;
        $data['item_id'] = $item_id;
        $data['cat_id'] = $cat_id;
        $data['addtime'] = time();
        $data['author_uid'] = $login_user['uid'];
        $data['author_username'] = $login_user['username'];
        
        if ($page_id > 0) {
            
            // 在保存前先把当前页面的版本存档
            $page = D("Page")->where(" page_id = '$page_id' ")->find();
            $insert_history = array(
                'page_id' => $page['page_id'],
                'item_id' => $page['item_id'],
                'cat_id' => $page['cat_id'],
                'page_title' => $page['page_title'],
                'page_content' => $page['page_content'],
                'order' => $page['order'],
                'addtime' => $page['addtime'],
                'author_uid' => $page['author_uid'],
                'author_username' => $page['author_username']
            );
            D("PageHistory")->add($insert_history);
            
            $ret = D("Page")->where(" page_id = '$page_id' ")->save($data);
            
            // 更新项目时间
            D("Item")->where(" item_id = '$item_id' ")->save(array(
                "last_update_time" => time()
            ));
            
            $return = D("Page")->where(" page_id = '$page_id' ")->find();
        } else {
            
            $page_id = D("Page")->add($data);
            
            // 更新项目时间
            D("Item")->where(" item_id = '$item_id' ")->save(array(
                "last_update_time" => time()
            ));
            
            $return = D("Page")->where(" page_id = '$page_id' ")->find();
        }
        if (! $return) {
            $return['error_code'] = 10103;
            $return['error_message'] = 'request  fail';
        }
        $this->sendResult($return);
    }
    
    // 删除页面
    public function delete()
    {
        $page_id = I("page_id/d") ? I("page_id/d") : 0;
        $page = D("Page")->where(" page_id = '$page_id' ")->find();
        
        $login_user = $this->checkLogin();
        if (! $this->checkItemCreator($login_user['uid'], $page['item_id']) && $login_user['uid'] != $page['author_uid']) {
            $this->message("你无权限！此页面由" . $page['author_username'] . "创建");
            return;
        }
        
        if ($page) {
            
            $ret = D("Page")->where(" page_id = '$page_id' ")
                ->limit(1)
                ->delete();
            // 更新项目时间
            D("Item")->where(" item_id = '$page[item_id]' ")->save(array(
                "last_update_time" => time()
            ));
        }
        if ($ret) {
            $this->message("删除成功！", U("Home/item/show", array(
                'item_id' => $page['item_id']
            )));
        } else {
            $this->message("删除失败！", U("Home/item/show", array(
                'item_id' => $page['item_id']
            )));
        }
    }
    
    // 历史版本
    public function history()
    {
        $page_id = I("page_id/d") ? I("page_id/d") : 0;
        $this->assign("page_id", $page_id);
        
        $PageHistory = D("PageHistory")->where("page_id = '$page_id' ")
            ->order(" addtime desc")
            ->limit(10)
            ->select();
        
        if ($PageHistory) {
            foreach ($PageHistory as $key => &$value) {
                $value['addtime'] = date("Y-m-d H:i:s", $value['addtime']);
            }
        }
        
        $this->assign("PageHistory", $PageHistory);
        
        $this->display();
    }
    
    // 导出word
    public function word()
    {
        import("Vendor.Parsedown.Parsedown");
        $Parsedown = new \Parsedown();
        $page = D("Page")->where("page_id = " . intval($_GET['page_id']))->find();
        $data = '';
        $data .= "<h1>{$page['page_title']}</h1>";
        $data .= '<div style="margin-left:20px;">';
        $data .= htmlspecialchars_decode($Parsedown->text($page['page_content']));
        $data .= '</div>';
        output_word($data, $page['page_title'] . '-' . date('Y-m-d-H-i-s'));
    }

    public function downmd()
    {
        $page = D("Page")->where("page_id = " . intval($_GET['page_id']))->find();
        $date = date("Y-m-d-H-i-s");
        Header("Content-type:  application/octet-stream ");
        header("Content-Disposition:  attachment;  filename= {$page["page_title"]}-{$date}.md");
        echo $page['page_content'];
    }
    // 上传图片
    public function uploadImg()
    {
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->allowExts = array(
            'jpg',
            'gif',
            'png',
            'jpeg'
        ); // 设置附件上传类型
        $upload->rootPath = './Public/Uploads/'; // 设置附件上传目录
        $upload->savePath = ''; // 设置附件上传子目录
        $info = $upload->upload();
        if (! $info) { // 上传错误提示错误信息
            $this->error($upload->getError());
            return;
        } else { // 上传成功 获取上传文件信息
            
            $url = get_domain() . __ROOT__ . substr($upload->rootPath, 1) . $info['editormd-image-file']['savepath'] . $info['editormd-image-file']['savename'];
            echo json_encode(array(
                "url" => $url,
                "success" => 1
            ));
        }
    }
}